package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Menu;
import cn.wolfcode.qo.MenuQueryObject;
import cn.wolfcode.service.IMenuService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.util.PermissionAnnotation;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;

@Controller
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private IMenuService menuService;


    @PermissionAnnotation(name="菜单页面",expression="menu:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") MenuQueryObject qo){
        PageInfo<Menu> pageInfo = menuService.query(qo);
        model.addAttribute("PageInfo", pageInfo);
        return "menu/list";
    }


    @PermissionAnnotation(name="菜单删除",expression="menu:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           menuService.delete(id);
        }
        return "redirect:/menu/list";
    }


    @PermissionAnnotation(name="菜单新增/编辑",expression="menu:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Menu menu) {
        if (menu.getId() == null) {
            menuService.save(menu);
        } else {
            menuService.update(menu);
        }
        return "redirect:/menu/list";
    }


    @PermissionAnnotation(name="菜单新增/编辑",expression="menu:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("menu",menuService.get(id));
        }
        return "menu/input";
    }
}
